package com.panlm.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class ZKTest {

    private static String connectString = "hadoop102:2181,hadoop103:2181,hadoop103:2181";
    private static int sessionTimeout = 2000;
    private ZooKeeper zkClent = null;

    //1创建ZooKeeper客户端
    @Before
    public void init() throws IOException {
        zkClent = new ZooKeeper(connectString, sessionTimeout, event -> {
            // 收到事件通知后的回调函数（用户的业务逻辑）
            System.out.println(event.getType() + "--" + event.getPath());
            // 再次启动监听
            try {
                zkClent.getChildren("/", true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    //创建子节点
    @Test
    public void create() throws Exception {
        // 参数1：要创建的节点的路径； 参数2：节点数据 ； 参数3：节点权限 ；参数4：节点的类型
        zkClent.create("/guilin", "quanzhou".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    //获取子节点并监听节点变化
    @Test
    public void getChildren() throws InterruptedException, KeeperException {
        List<String> children = zkClent.getChildren("/", true);
        for (String child : children) {
            System.out.println(child);
        }

        // 延时阻塞
        Thread.sleep(Long.MAX_VALUE);
    }

    // 判断znode是否存在
    @Test
    public void exist() throws InterruptedException, KeeperException {
        Stat stat = zkClent.exists("/guilin", false);
        System.out.println(stat == null ? "not exist" : "exist");
    }
}
